package com.yg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yg.config.result.R;
import com.yg.entity.readAndWriteTemplates.TeacherAppointTemplate;
import com.yg.entity.singleEntity.Dictionary;
import com.yg.entity.singleEntity.Teacher;
import com.yg.entity.singleEntity.TeacherAppoint;
import com.yg.service.DictionaryService;
import com.yg.service.TeacherAppointService;
import com.yg.service.TeacherService;
import com.yg.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 对应sheet03，此表为教师挂职表
 * 注意！此表的id和教师表的id是一个id 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@Api("对应sheet03，此表为教师挂职表")
@RestController
@RequestMapping("/teacherAppoint")
@CrossOrigin
public class TeacherAppointController {

    @Autowired
    private TeacherAppointService teacherAppointService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 添加教师挂职信息
     *
     * @param appointTemplate 教师挂职信息
     * @param request
     * @return 返回值
     */
    @ApiOperation("添加教师挂职信息")
    @PostMapping("postTeacherAppoint")
    public R addTeacherAppoint(
            @ApiParam(name = "teacherAppoint", value = "教师", required = false) @RequestBody(required = false) TeacherAppointTemplate appointTemplate,
            @ApiParam(name = "httpRequest", value = "HttpServletRequest", required = true) HttpServletRequest request
    ) {

        Teacher one = null;
        try {
            QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
            wrapper.eq("teacher_id", appointTemplate.getTeacherId());
            wrapper.eq("name", appointTemplate.getName());
            one = teacherService.getOne(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("此教师信息查询错误");
        }

        if (one != null) {
            try {
                TeacherAppoint teacherAppoint = new TeacherAppoint();

                BeanUtils.copyProperties(appointTemplate, teacherAppoint);

                System.out.println(teacherAppoint.toString());
                teacherAppoint.setId(one.getId());
                teacherAppointService.save(teacherAppoint);

                return R.OK().message("插入成功");

            } catch (BeansException e) {
                e.printStackTrace();
                return R.ERROR().message("插入失败");
            }
        } else {
            return R.ERROR().message("查询不到此教师信息");
        }


    }

    /**
     * 删除教师挂职信息
     *
     * @param appointTemplate 教师挂职信息
     * @return
     */
    @ApiOperation("删除教师挂职信息")
    @DeleteMapping("deleteTeacherAppoint")
    public R deleteTeacherAppoint(
            @RequestBody(required = false) TeacherAppointTemplate appointTemplate
    ) {
        System.out.println(appointTemplate.toString());
        try {
            String id = appointTemplate.getId();
            teacherAppointService.removeById(id);

            return R.OK().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("删除失败");
        }
    }

    /**
     * 修改教师挂职信息
     *
     * @param appointTemplate 教师信息
     * @return
     */
    @ApiOperation("修改教师挂职信息")
    @PutMapping("putTeacherAppoint")
    public R updateTeacherAppoint(
            @RequestBody TeacherAppointTemplate appointTemplate
    ) {
        TeacherAppoint teacherAppoint = new TeacherAppoint();
        BeanUtils.copyProperties(appointTemplate, teacherAppoint);
        teacherAppointService.saveOrUpdate(teacherAppoint);
        return R.OK().message("修改成功");
    }

    /**
     * 查询教师的挂职信息
     *
     * @param current         当前页
     * @param limit           每页显示的条目
     * @param appointTemplate 条件
     * @param request         HttpServletRequest
     * @return
     */
    @ApiOperation("查询教师的挂职信息")
    @PostMapping("selectTeacherAppointByCondition")
    public R getTeacherAppoint(
            @ApiParam(name = "current", value = "当前页", required = false) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = false) @RequestParam(name = "limit") long limit,
            @ApiParam(name = "teacher", value = "教师的查询条件", required = false) @RequestBody(required = false) TeacherAppointTemplate appointTemplate,
            @ApiParam(name = "httpRequest", value = "HttpServletRequest", required = true) HttpServletRequest request
    ) {
        String vip = JwtUtils.getVipByJwtToken(request);
        String teacherId = JwtUtils.getTeacherIdByJwtToken(request);
        R r = teacherAppointService.getTeacherAppoint((current - 1) * limit, limit, appointTemplate, vip, teacherId);

        return r;
    }

    /**
     * 查询所有教师的挂职信息
     *
     * @param current 当前页
     * @param limit   每页显示的条目
     * @param request HttpServletRequest
     * @return
     */
    @ApiOperation("查询所有教师的挂职信息")
    @GetMapping("getAllTeacherAppoint")
    public R getAllTeacherAppoint(
            @ApiParam(name = "current", value = "当前页", required = true) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = true) @RequestParam(name = "limit") long limit,
            @ApiParam(name = "httpRequest", value = "HttpServletRequest", required = true) HttpServletRequest request
    ) {
        String vip = JwtUtils.getVipByJwtToken(request);
        String teacherId = JwtUtils.getTeacherIdByJwtToken(request);
        R r = teacherAppointService.getTeacherAppoint((current - 1) * limit, limit, null, vip, teacherId);

        List<Dictionary> title = dictionaryService.getTitle();
        List<Dictionary> degree = dictionaryService.getDegree();
        List<Dictionary> education = dictionaryService.getEducation();

        return r.data("title", title).data("degree", degree).data("education", education);
    }


    /**
     * 从表格读取，写入数据库
     *
     * @param file
     * @return
     */
    @ApiOperation("从表格读取，写入数据库")
    @PostMapping("readExcel")
    public R readExcel(@ApiParam(name = "file", value = "要上传的文件表格", required = true) MultipartFile file) {

        R r = R.OK();

        teacherAppointService.readExcel(file, r);

        return r;
    }


    /**
     * 导出数据库数据到表格中
     */
    @ApiOperation("导出数据库数据到表格中")
    @GetMapping("writeExcel")
    public void writeExcel(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "title", required = false) String title,
            HttpServletResponse response
    ) {
        TeacherAppointTemplate appointTemplate = new TeacherAppointTemplate();
        appointTemplate.setName(name);
        appointTemplate.setTitle(title);

        teacherAppointService.writeExcel(appointTemplate, response);
    }
}